package org.kabeja.parser.entities;

import org.kabeja.dxf.entity.DXFEntity;
import org.kabeja.dxf.entity.DXFPolyline;
import org.kabeja.dxf.entity.DXFVertex;
import org.kabeja.parser.DXFValue;

/**
 * @author shutao
 * @since 2022/4/25 11:40
 */
public class DXFPolylineHandler extends AbstractEntityHandler {
    public static final String ENTITY_NAME = "POLYLINE";
    public static final String ENTITY_VERTEX = "VERTEX";
    public static final String END_SEQUENCE = "SEQEND";
    public static final int END_SEQUENCE_CODE = -2;
    public static final int VERTEX_BULGE = 42;
    public static final int START_WIDTH = 40;
    public static final int END_WIDTH = 41;
    public static final int THICKNESS = 39;
    public static final int SURFACE_TYPE = 75;
    public static final int SUREFACE_DENSITY_ROW_COUNT = 73;
    public static final int SUREFACE_DENSITY_COLUMN_COUNT = 74;
    public static final int ROW_COUNT = 71;
    public static final int COLUMN_COUNT = 72;
    private boolean follow = true;
    private boolean parse_vertex = false;
    private DXFVertex vertex;
    private DXFPolyline polyline;

    public DXFPolylineHandler() {
    }

    @Override
    public void endDXFEntity() {
    }

    @Override
    public DXFEntity getDXFEntity() {
        return this.polyline;
    }

    @Override
    public String getDXFEntityName() {
        return "POLYLINE";
    }

    @Override
    public boolean isFollowSequence() {
        return this.follow;
    }

    @Override
    public void parseGroup(int var1, DXFValue var2) {
        if (var1 != -2 && !"SEQEND".equals(var2.getValue())) {
            switch(var1) {
                case 0:
                    if ("VERTEX".equals(var2.getValue())) {
                        if (this.parse_vertex) {
                            this.polyline.addVertex(this.vertex);
                        } else {
                            this.parse_vertex = true;
                        }

                        this.vertex = new DXFVertex();
                        this.vertex.setDXFDocument(this.doc);
                    }
                    break;
                case 10:
                    if (this.parse_vertex) {
                        this.vertex.setX(var2.getDoubleValue());
                    }
                    break;
                case 20:
                    if (this.parse_vertex) {
                        this.vertex.setY(var2.getDoubleValue());
                    }
                    break;
                case 30:
                    if (this.parse_vertex) {
                        this.vertex.setZ(var2.getDoubleValue());
                    }
                    break;
                case 39:
                    this.polyline.setThickness(var2.getDoubleValue());
                    break;
                case 40:
                    if (this.parse_vertex) {
                        this.vertex.setStartWidth(var2.getDoubleValue());
                    } else {
                        this.polyline.setStartWidth(var2.getDoubleValue());
                    }
                    break;
                case 41:
                    if (this.parse_vertex) {
                        this.vertex.setEndWidth(var2.getDoubleValue());
                    } else {
                        this.polyline.setEndWidth(var2.getDoubleValue());
                    }
                    break;
                case 42:
                    if (this.parse_vertex) {
                        this.vertex.setBulge(var2.getDoubleValue());
                    }
                    break;
                case 71:
                    if (this.parse_vertex) {
                        this.vertex.setPolyFaceMeshVertex0(var2.getIntegerValue());
                    } else {
                        this.polyline.setRows(var2.getIntegerValue());
                    }
                    break;
                case 72:
                    if (this.parse_vertex) {
                        this.vertex.setPolyFaceMeshVertex1(var2.getIntegerValue());
                    } else {
                        this.polyline.setColumns(var2.getIntegerValue());
                    }
                    break;
                case 73:
                    if (this.parse_vertex) {
                        this.vertex.setPolyFaceMeshVertex2(var2.getIntegerValue());
                    } else {
                        this.polyline.setSurefaceDensityRows(var2.getIntegerValue());
                    }
                    break;
                case 74:
                    if (this.parse_vertex) {
                        this.vertex.setPolyFaceMeshVertex3(var2.getIntegerValue());
                    } else {
                        this.polyline.setSurefaceDensityColumns(var2.getIntegerValue());
                    }
                    break;
                case 75:
                    this.polyline.setSurefaceType(var2.getIntegerValue());
                    break;
                default:
                    if (this.parse_vertex) {
                        super.parseCommonProperty(var1, var2, this.vertex);
                    } else {
                        super.parseCommonProperty(var1, var2, this.polyline);
                    }
            }

        } else {
            this.polyline.addVertex(this.vertex);
            this.follow = false;
        }
    }

    @Override
    public void parseObject(int var1, DXFValue var2, int type) {
        if (this.parse_vertex) {
            super.parseObjectProperty(var1, var2, type, this.vertex);
        } else {
            super.parseObjectProperty(var1, var2, type, this.polyline);
        }
    }

    @Override
    public void startDXFEntity() {
        this.follow = true;
        this.parse_vertex = false;
        this.polyline = new DXFPolyline();
    }
}
